/**
 * 
 */
package com.asset.salesforce.controller.pages;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.EmployeeService;

// TODO: Auto-generated Javadoc
/**
 * The Class ContactController.
 *
 * @author Hisham
 */
@Controller
public class ContactController {

	/** The contact service. */
	@Autowired
	private ContactService contactService;
	
	/** The employee service. */
	@Autowired
	private EmployeeService employeeService;
	
	/**
	 * Gets the contacts page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @return the contacts page
	 */
	@RequestMapping(value = "/contacts", method = RequestMethod.GET)
	public String getContactsPage(HttpSession session, Model model,HttpServletResponse response) {
		if (null == session.getAttribute("userDetails")) {return "redirect:login";}
		return "contactsPage";
	}
	
	/**
	 * Gets the new contact page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @return the new contact page
	 */
	@RequestMapping(value = "/newContact", method = RequestMethod.GET)
	public String getNewContactPage(HttpSession session, Model model,HttpServletResponse response) {
		if (null == session.getAttribute("userDetails")) {return "redirect:login";}
		if (SecurityUtils.getSubject().hasRole("contactAdd")){
			model.addAttribute("contactDTO",new Contact());
			return "createContactPage";
		}
		else{
			return "accessDenied";
		}
	}
	
	/**
	 * Post new contact.
	 *
	 * @param model the model
	 * @param session the session
	 * @param contactForm the contact form
	 * @param result the result
	 * @param request the request
	 * @param response the response
	 * @return the string
	 */
	@RequestMapping(value="/newContact" ,method=RequestMethod.POST)
	public String postNewContact(Model model ,HttpSession session,@Valid @ModelAttribute("contactDTO") Contact contactForm,BindingResult result,
								HttpServletRequest request,HttpServletResponse response){
		if (null == session.getAttribute("userDetails")) {return "redirect:login";}
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		
		Contact contact = new Contact();
		contact.setCreator(userDetails);
		if(contactForm.getParentName() != null && !contactForm.getParentName().equalsIgnoreCase("")){
			contact.setParentName(contactForm.getParentName());
		}
		if(contactForm.getParentCompany() != null && !contactForm.getParentCompany().equalsIgnoreCase("")){
			contact.setParentCompany(contactForm.getParentCompany());
		}
		if(contactForm.getViewType() != null && !contactForm.getViewType().equalsIgnoreCase("")){
			contact.setViewType(contactForm.getViewType());
		}
		if(contactForm.getParentId() != null){
			contact.setParentId(contactForm.getParentId());
		}
		contact.setDateCreated(new Date());
		if(contactForm.getEmail() != null && !contactForm.getEmail().equalsIgnoreCase("")){
			contact.setEmail(contactForm.getEmail());
		}
		if(contactForm.getMobile() != null && !contactForm.getMobile().equalsIgnoreCase("")){
			contact.setMobile(contactForm.getMobile());
		}
		if(contactForm.getPhone() != null && !contactForm.getPhone().equalsIgnoreCase("")){
			contact.setPhone(contactForm.getPhone());
		}
		if(contactForm.getTitle() != null && !contactForm.getTitle().equalsIgnoreCase("")){
			contact.setTitle(contactForm.getTitle());
		}
		if(contactForm.getfName() != null && !contactForm.getfName().equalsIgnoreCase("")){
			contact.setfName(contactForm.getfName());
		}
		if(contactForm.getlName() != null && !contactForm.getlName().equalsIgnoreCase("")){
			contact.setlName(contactForm.getlName());
		}
		contact.setIsDeleted(false);
		contactService.add(contact);
		
		return "redirect:contacts";
	}
	
	/**
	 * Edits the contact.
	 *
	 * @param model the model
	 * @param session the session
	 * @param request the request
	 * @param response the response
	 * @param id the id
	 * @return the string
	 */
	@RequestMapping(value="/editContact",method=RequestMethod.GET)
	public String editContact(Model model ,HttpSession session,HttpServletRequest request,HttpServletResponse response
							,@RequestParam Integer id){
			if (SecurityUtils.getSubject().hasRole("contactEdit")){
			Contact contact = contactService.getById(id);

			model.addAttribute("contactDTO", contact);
			return "editContactPage";
			}
			else{return "accessDenied";}
	}
	
	/**
	 * Gets the view contact page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @param request the request
	 * @param id the id
	 * @return the view contact page
	 */
	@RequestMapping(value = "/viewContact", method = RequestMethod.GET)
	public String getViewContactPage(HttpSession session, Model model,HttpServletResponse response,HttpServletRequest request,@RequestParam Integer id) {
		if (SecurityUtils.getSubject().hasRole("contactView")){
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:login";
			}
			Contact contact = contactService.getById(id);
			model.addAttribute("contactDTO",contact);
			return "viewContactPage";
			}
			else{
				return "accessDenied";
			}
	}
}
